﻿(function($) {
    var that;
    var service = function() {
        that = this;
        this.serviceMap = {
            GetTripsList: {
                url: $bw.config.api + "Trips/GetTripsList", //通过用户信息获取航班信息
                type: "POST",
                dataType: "json",
                headers: {
                    "Content-Type": "application/json; charset=utf-8"
                }
            },
            AddBookingByWeChat: {
                url: $bw.config.api + "BookingList/AddBookingByWeChat", //微信端预约 
                type: "POST",
                dataType: "json"
            },
            GetOrderItems: {
                url: $bw.config.api + "BookingList/GetOrderItems", //通过openId或者是用户证件号来查询预约记录 
                type: "POST",
                dataType: "json"
            },
            GetBookingInfo: {
                url: $bw.config.api + "BookingList/GetBookingInfo", //通过获取预约订单的详细信息 
                type: "GET",
                dataType: "json"
            },
            GetCityAirportList: {
                url: $bw.config.api + "CityAirport/GetCityAirportList", //模糊搜索三字码数据
                type: "GET",
                dataType: "json"
            },
            GetToken: {
                url: $bw.config.api + "Weixin/GetToken", //通过微信授权得到code，从后台获取access_token、openid
                type: "GET",
                dataType: "json"
            },
            GetSuggestionAddress: {
                url: "http://api.map.baidu.com/place/v2/suggestion", //通过百度地图模糊匹配地址并返回匹配的集合
                type: "GET",
                dataType: "json"
            },
            GetTrackHistory: {
                url: "http://api.map.baidu.com/trace/v2/track/gethistory", //通过百度鹰眼API获取某一个监控对象[entity]一天内的历史轨迹点集合
                type: "GET",
                dataType: "json"
            },
            GetentityList: {
                url: "Weixin/GetToken", //通过百度鹰眼API获取当前系统中所有的监控对象 [entity]的集合
                type: "GET",
                dataType: "json"
            }
        };

        service.prototype.GetTripsList = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetTripsList"));
        }
        service.prototype.AddBookingByWeChat = function(param, callback) {
            $.ajax(createOptions(param, callback, "AddBookingByWeChat"));
        }
        service.prototype.GetOrderItems = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetOrderItems"));
        }
        service.prototype.GetBookingInfo = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetBookingInfo"));
        }
        service.prototype.GetToken = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetToken"));
        }
        service.prototype.GetSuggestionAddress = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetSuggestionAddress"));
        }
        service.prototype.GetTrackHistory = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetTrackHistory"));
        }
        service.prototype.GetCityAirportList = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetCityAirportList"));
        }
        service.prototype.GetentityList = function(param, callback) {
            $.ajax(createOptions(param, callback, "GetentityList"));
        }

        function createOptions(param, callback, _name) {
            var _param = {
                data: that.serviceMap[_name].type.toUpperCase() === "POST" ? JSON.stringify(param) : param,
                success: function(data) {
                    callback && callback(data);
                },
                error: function(xhr, type) {
                    callback && callback(false);
                }
            }
            return $.extend(_param, that.serviceMap[_name]);
        }
    }
    window.$service = new service();
})($);
